package com.wl.index.Biz.Impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.entity.Note;
import com.wl.index.Biz.INoteBiz;
import com.wl.index.Mapper.NoteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoteBizImpl extends ServiceImpl<NoteMapper,Note> implements INoteBiz {
    @Autowired
    private NoteMapper noteMapper;

    @Override
    public List<Note> selectByTitle(String title) {
        return noteMapper.selectByTitle(title);
    }

    @Override
    public Note selectNoteauthorById(Integer nid) {
        return noteMapper.selectNoteauthorById(nid);
    }

    @Override
    public Note selectUserByUid(Integer uid) {
        return noteMapper.selectUserByUid(uid);
    }

    @Override
    public List<Note> selectAllValidNotes(Integer uid) {
        return noteMapper.selectAllValidNotes(uid);
    }

    @Override
    public int insertNote(Note note) {
        if (StringUtils.isBlank(note.getTitle())) {
            throw new IllegalArgumentException("标题不能为空");
        }

        if (note.getCreatedAt() == null) {
            note.setCreatedAt(new Date());
        }
        if (note.getUpdatedAt() == null) {
            note.setUpdatedAt(new Date());
        }
        if (note.getSta() == null) {
            note.setSta(0);
        }
        if (note.getViewCount() == null) {
            note.setViewCount(0);
        }
        if (note.getLikeCount() == null) {
            note.setLikeCount(0);
        }
        if (note.getFavoriteCount() == null) {
            note.setFavoriteCount(0);
        }
        if (note.getCommentCount() == null) {
            note.setCommentCount(0);
        }

        // 插入数据
        return noteMapper.insertNote(note);
    }

}
